<template>
  <div class="mod-config">
    <el-button type="primary" @click="burchaseSubordinateUpdateHandle()">新增</el-button>
    <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        style="width: 100%">
        <el-table-column
          prop="className"
          label="分类名">
        </el-table-column>
        <el-table-column
          prop="sequence"
          header-align="center"
          align="center"
          label="排序">
        </el-table-column>
        <el-table-column
          prop="goodsNum"
          header-align="center"
          align="center"
          label="商品数">
        </el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          width="300"
          label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="addGoods(scope.row.id)">设置商品</el-button>
            <el-button type="text" size="small" @click="getGoods(scope.row.id)">查看商品</el-button>
            <el-button type="text" size="small" @click="burchaseSubordinateUpdateHandle(scope.row.id)">修改</el-button>
            <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <burchase-subordinate-add-or-update v-if="burchaseSubordinateAddOrUpdateVisible" ref="burchaseSubordinateAddOrUpdate" @refreshDataList="init"></burchase-subordinate-add-or-update>
    <add-goods v-if="addGoodsVisible" ref="addGoods" @refreshDataList="init"></add-goods>
    <get-goods v-if="getGoodsVisible" ref="getGoodsList" @refreshDataList="init"></get-goods>
  </div>
</template>

<script>
  import BurchaseSubordinateAddOrUpdate from './shop-class-add-or-update'
  import AddGoods from './shop-goods'
  import GetGoods from './shop-get-goods'
  export default {
    data () {
      return {
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataList: [],
        dataForm: {
          id: 0,
          sequence: '',
          className: ''
        },
        dataListLoading: false,
        addGoodsVisible: false,
        getGoodsVisible: false,
        burchaseSubordinateAddOrUpdateVisible: false,
        dataRule: {
          className: [
            { required: true, message: '分类名称不能为空', trigger: 'blur' }
          ],
          sequence: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    components: {
      BurchaseSubordinateAddOrUpdate,
      AddGoods,
      GetGoods
    },
    methods: {
      init (id) {
        debugger
        this.getDataList1(null)
      },
      getDataList1 (id) {
        if (id != null) {
          this.dataForm.id = id
        }
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/shopTemplate/getShopClassListById'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'keywords': this.dataForm.id
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.data.list
            this.totalPage = data.data.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.init(this.dataForm.id)
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.init(this.dataForm.id)
      },
      burchaseSubordinateUpdateHandle (id) {
        this.burchaseSubordinateAddOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.burchaseSubordinateAddOrUpdate.init(id, this.dataForm.id)
        })
      },
      addGoods (id) {
        this.addGoodsVisible = true
        this.$nextTick(() => {
          this.$refs.addGoods.init(id)
        })
      },
      getGoods (id) {
        this.getGoodsVisible = true
        this.$nextTick(() => {
          this.$refs.getGoodsList.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定删除操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/shopTemplate/removeShopClassById'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.init(this.dataForm.id)
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
